package logic

import (
	"context"
	"github.com/pkg/errors"
	"mall/app/member/cmd/rpc/pb"
	"mall/common/xerr"

	"mall/app/auth/cmd/api/internal/svc"
	"mall/app/auth/cmd/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LoginLogic) Login(req *types.LoginReq) error {
	_, err := l.svcCtx.MemberRpc.Login(l.ctx, &pb.LoginReq{
		Phone:    req.Phone,
		Password: req.Password,
	})
	if err != nil {
		return errors.Wrapf(xerr.NewErrorWithCode(xerr.UnamePwdErr), "用户名密码错误")
	}
	return nil
}
